<template>
  <el-drawer v-model="drawerVisible" :z-index="1000" :destroy-on-close="true" :title="`${drawerProps.title}`" size="1000px">
    <div class="roomDrawer">
      <div class="tree" v-loading="loading1">
        <ZTree
          :id="treeId"
          :zNodes="drawerProps.zNodes"
          :checkNodeId="drawerProps.row!.mapndx"
          @click-dom="zTreeOnClick"
          v-if="isShow"
        />
      </div>
      <el-form
        class="roomForm"
        ref="RoomFormRef"
        label-width="110px"
        label-suffix=" :"
        :rules="rules"
        :disabled="drawerProps.isView"
        :model="drawerProps.row"
        :hide-required-asterisk="drawerProps.isView"
      >
        <el-form-item label="智能锁位置">
          <el-input v-model="roomMapName" placeholder="请填写锁地图名称" readonly clearable></el-input>
        </el-form-item>
        <el-form-item label="房间名称" prop="roomName">
          <el-input v-model="drawerProps.row!.roomName" placeholder="请填写房间名称" clearable></el-input>
        </el-form-item>

        <el-form-item label="房间编号" prop="roomCode">
          <el-input v-model="drawerProps.row!.roomCode" placeholder="请填写房间编号" clearable></el-input>
        </el-form-item>

        <el-form-item label="锁编号" prop="lockCode">
          <el-input v-model="drawerProps.row!.lockCode" placeholder="请填写锁编号" clearable></el-input>
        </el-form-item>

        <el-form-item label="酒店名称" prop="hotelId">
          <el-select v-model="drawerProps.row!.hotelId" placeholder="请选择酒店" clearable>
            <el-option v-for="item in hotelSelList" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>

        <el-form-item label="网关名称" prop="gatewayId">
          <el-select v-model="drawerProps.row!.gatewayId" placeholder="请选择网关" clearable>
            <el-option v-for="item in gatewaySelList" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="锁状态" prop="onlineStatus">
        <el-select v-model="drawerProps.row!.onlineStatus" placeholder="请选择锁状态" clearable>
          <el-option v-for="item in connectStatus" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="电量" prop="electricity">
        <el-input v-model="drawerProps.row!.electricity" placeholder="请填写电量" clearable></el-input>
      </el-form-item> -->
      </el-form>
    </div>
    <template #footer>
      <el-button @click="_colse">取消</el-button>
      <el-button type="primary" :loading="loading" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="RoomDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import ZTree from "@/components/ZTree/index.vue";
import { getHotelSelList, getGatewaySelList } from "@/api/modules/smartLock/manage/electronMap";
// import { connectStatus } from "@/utils/serviceDict";

const treeId = ref("tree1");
const loading = ref(false);
const loading1 = ref(false);
const isShow = ref(false);
const hotelSelList = ref([]);
const gatewaySelList = ref([]);
const roomMapName = ref("");

const rules = reactive({
  roomName: [{ required: true, message: "请填写房间名称" }],
  roomCode: [{ required: true, message: "请填写房间编号" }],
  lockCode: [
    { required: true, message: "请填写锁编号" },
    {
      min: 8,
      max: 8,
      message: "房间编号长度必须为8位"
    }
  ],
  hotelId: [{ required: true, message: "请选择酒店" }],
  gatewayId: [{ required: true, message: "请选择网关" }]
  // onlineStatus: [{ required: true, message: "请选择锁状态" }],
  // electricity: [{ required: true, message: "请填写电量" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: any;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  resetTableList?: () => void;
  zNodes: { [key: string]: any }[];
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {},
  zNodes: []
});
// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  loading1.value = true;
  isShow.value = false;
  setTimeout(() => {
    loading1.value = false;
    isShow.value = true;
  }, 500);
  getHotelSelData();
  getGatewaySelData();
};

// 提交数据（新增/编辑）
const RoomFormRef = ref<FormInstance>();
/**
 * 关闭弹窗
 */
const _colse = () => {
  drawerVisible.value = false;
  RoomFormRef.value && RoomFormRef.value.resetFields();
};

/**
 * 点击树节点
 */
const zTreeOnClick = ({ treeNode }) => {
  drawerProps.value.row.mapndx = treeNode.id;
  roomMapName.value = treeNode.name;
};

/**
 * 获取酒店选择列表
 */
const getHotelSelData = async () => {
  try {
    const res = await getHotelSelList();
    hotelSelList.value = res.data;
  } catch (error) {
    console.log(error);
  }
};
/**
 * 获取网关选择列表
 */
const getGatewaySelData = async () => {
  try {
    const res = await getGatewaySelList();
    gatewaySelList.value = res.data;
  } catch (error) {
    console.log(error);
  }
};
/**
 * 提交信息
 */
const handleSubmit = () => {
  RoomFormRef.value!.validate(async valid => {
    if (!valid) return;
    let params = {
      ...drawerProps.value.row
    };
    if (drawerProps.value.title === "新增") params.onlineStatus = 0;
    loading.value = true;
    try {
      await drawerProps.value.api!(params);
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.title === "新增" ? drawerProps.value.resetTableList!() : drawerProps.value.getTableList!();
      drawerVisible.value = false;
      loading.value = false;
    } catch (error) {
      loading.value = false;
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
<style lang="scss" scoped>
.roomDrawer {
  display: flex;
  height: 100%;
  padding: 0 20px;
  .tree {
    width: 320px;
    height: 100%;
    .ztree {
      height: 100%;
      overflow-y: auto;
    }
  }
  .roomForm {
    flex: 1;
    padding: 10px 20px 0;
    overflow-y: auto;
  }
}
</style>
